package com.zk.junit4;

import cn.hutool.core.util.ReflectUtil;
import com.zk.AppTest;
import com.zk.util.ClassUtil;
import org.junit.internal.JUnitSystem;
import org.junit.internal.RealSystem;
import org.junit.internal.TextListener;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunListener;
import org.junit.runners.JUnit4;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

import java.util.List;
import java.util.Set;

/**
 * 基于JunitCore，自己写Runner，可以不使用IDEA的集成插件。
 */
public class JunitCoreRunDemo {


    public static void main(String[] args) throws Exception {
        Set<Class<?>> classes = ClassUtil.getClasses("com.zk");

        ClassLoader classLoader = ClassLoader.getSystemClassLoader();

        JUnit4 junit4 = new JUnit4(AppTest.class);
        Filter filter = new Filter() {
            @Override
            public boolean shouldRun(Description description) {
                System.out.println("Class Name:" + description.getClass().getName());
                System.out.println(description);
                return true;
            }

            @Override
            public String describe() {
                return "Run.";
            }
        };

        List<FrameworkMethod> children = ReflectUtil.invoke(junit4, "getFilteredChildren");

        System.out.println("Children:" + children);

        filter.apply(junit4);

        JUnitCore junitCore = new JUnitCore();
//        JUnitSystem system = new RealSystem();
//        RunListener runListener = new TextListener(system);

        RunListener runListener = new Junit4DemoRunListener();
        junitCore.addListener(runListener);
        junitCore.run(junit4);

    }

}
